/*file: createPrime.gp*/


/*s: how many bit is the prime*/
searchPrime(s) = 
{
	global(f);
	/*
		p := prime,
		(p-1)'s greatest factor is greater than
		2^s, => p must be at least 2*(2^s)
	*/
	nextPrimeLimit = 2*(2^s);
	p = nextPrimeLimit;

	TRUE  = 1;
	FALSE = 0;
	continues = TRUE;
	while (continues,
		p = nextprime(p);
		f = factor(p-1);
		/*
		print ("f:");
		print (f);
		*/

		lastF = f[ length(f [,1]) , 1];
		/*
		print ("last F");	
		print (lastF);
		*/

		if( lastF > 2^s,
			break();
			,/*else*/
			p = p*round(log(p));	
			\\p = p+1;
		)
	);
	return (p);
}

/* test speed of searchPrime
 * r: number of test-rounds 
 * s: number of bits of prime
 */
testSearchPrime(r,s) =
{
	local(x,p);
	forstep (x = 1, r, 1,
		t = gettime();
		p = searchPrime(s);
		z = round ( (gettime() - t) / 1000.0 );
		write("log.txt", s," ",p," " , z  ) ;
	)
}

printArray2PyFile(fileName,array,variableName) =
{
	l = length(array[,1]);
\\	printPreamble2PyFile(fileName);

	write (fileName, variableName, "=[");
	forstep(i =1, l, 1,
		m = length(array[1,]);
		write1 (fileName, "[");
		forstep(j = 1, m, 1,
			write1 (fileName,array[i,j]);
			if (j < m,
				write1 (fileName,",");
			);
		);
		write1 (fileName, "]");
		if (i < l, 
			write1 (fileName,",");
			,/*else*/
			write (fileName,"");
		);
	);
	write (fileName, "]");
}

printPreamble2PyFile(fileName) =
{
	preamble="# WARNING: This file is atomatically generated, do not edit it manual";
	write(fileName,preamble);
}

searchSafePrime(s) =
{
	global(f);
	/*
		p := prime,
		(p-1)'s greatest factor is greater than
		2^s, => p must be at least 2*(2^s)
	*/
	nextPrimeLimit = 2*(2^s);
	p = nextPrimeLimit;

	TRUE  = 1;
	FALSE = 0;
	continues = TRUE;
	while (continues,
		p = nextprime(p);
		f = factor(p-1);
		/*
		print ("f:");
		print (f);
		*/

		lastF = f[ length(f [,1]) , 1];
		/*
		print ("last F");	
		print (lastF);
		*/

		if( (lastF > (2^s)) && (lastF * 2 + 1 == p),
			break();
			,/*else*/
			p <<= 1 ;	
			\\p ++;
		)
	);
	return (p);
}


